home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / ivbsrc / testsort.frm < prev    next >
Text File  |  1995-05-08  |  3KB  |  112 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "Test sorted list box"
  4.    ClientHeight    =   2655
  5.    ClientLeft      =   1020
  6.    ClientTop       =   1425
  7.    ClientWidth     =   3405
  8.    Height          =   3060
  9.    Left            =   960
  10.    LinkMode        =   1  'Source
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   2655
  13.    ScaleWidth      =   3405
  14.    Top             =   1080
  15.    Width           =   3525
  16.    Begin TextBox Text2 
  17.       Height          =   375
  18.       Left            =   1560
  19.       TabIndex        =   4
  20.       Top             =   2160
  21.       Width           =   1695
  22.    End
  23.    Begin CommandButton Command2 
  24.       Caption         =   "End"
  25.       Height          =   495
  26.       Left            =   1560
  27.       TabIndex        =   6
  28.       Top             =   1560
  29.       Width           =   1695
  30.    End
  31.    Begin CommandButton Command1 
  32.       Caption         =   "Search"
  33.       Height          =   495
  34.       Left            =   120
  35.       TabIndex        =   3
  36.       Top             =   1560
  37.       Width           =   1335
  38.    End
  39.    Begin TextBox Text1 
  40.       Height          =   375
  41.       Left            =   1560
  42.       TabIndex        =   1
  43.       Top             =   1080
  44.       Width           =   1695
  45.    End
  46.    Begin ListBox List1 
  47.       Height          =   810
  48.       Left            =   120
  49.       Sorted          =   -1  'True
  50.       TabIndex        =   0
  51.       Top             =   120
  52.       Width           =   3135
  53.    End
  54.    Begin Label Label2 
  55.       Caption         =   "Search results:"
  56.       Height          =   255
  57.       Left            =   120
  58.       TabIndex        =   5
  59.       Top             =   2160
  60.       Width           =   1335
  61.    End
  62.    Begin Label Label1 
  63.       Caption         =   "Search item:"
  64.       Height          =   255
  65.       Left            =   120
  66.       TabIndex        =   2
  67.       Top             =   1080
  68.       Width           =   1215
  69.    End
  70. End
  71.  
  72. Function BinarySearch% (Ctrl As Control, Search$, LineNbr%)
  73.   If TypeOf Ctrl Is ListBox Then
  74.     NbrRecs% = Ctrl.ListCount
  75.     Found% = 0 'Item not found yet
  76.     LoNbr% = 0
  77.     HiNbr% = NbrRecs% - 1
  78.     Do
  79.       MidNbr% = (LoNbr% + HiNbr%) \ 2
  80.       If UCase$(Search$) < UCase$(Ctrl.List(MidNbr%)) Then 'Search the low portion of the list
  81.         HiNbr% = MidNbr% - 1
  82.       ElseIf UCase$(Search$) > UCase$(Ctrl.List(MidNbr%)) Then 'Search the high portion of the list
  83.         LoNbr% = MidNbr% + 1
  84.       Else 'Found it
  85.         Found% = -1
  86.         LineNbr% = MidNbr% 'Return record number
  87.       End If
  88.     Loop Until Found% Or (HiNbr% < LoNbr%)
  89.     BinarySearch% = Found% 'Return success code
  90.   End If
  91. End Function
  92.  
  93. Sub Command1_Click ()
  94.   S$ = Text1.Text
  95.   If BinarySearch%(list1, S$, L%) Then
  96.     Text2.Text = Str$(L%)
  97.   Else
  98.     Text2.Text = "Not Found"
  99.   End If
  100. End Sub
  101.  
  102. Sub Command2_Click ()
  103.   End
  104. End Sub
  105.  
  106. Sub Form_Load ()
  107.   list1.AddItem "ABD"
  108.   list1.AddItem "abc"
  109.   list1.AddItem "afk"
  110. End Sub
  111.  
  112.